package com.codeplatform.service;

import com.codeplatform.entity.CodeRecord;
import com.codeplatform.dto.CodeExecutionRequest;
import com.codeplatform.dto.CodeExecutionResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 代码记录服务接口
 * 
 * @author CodePlatform
 * @since 2024-01-01
 */
public interface CodeRecordService {

    /**
     * 保存代码记录
     * 
     * @param request 执行请求
     * @param result 执行结果
     * @return 保存的代码记录
     */
    CodeRecord saveCodeRecord(CodeExecutionRequest request, CodeExecutionResult result);

    /**
     * 根据ID获取代码记录
     * 
     * @param id 记录ID
     * @return 代码记录
     */
    Optional<CodeRecord> getCodeRecord(Long id);

    /**
     * 根据ID获取代码记录（包含权限检查）
     * 
     * @param id 记录ID
     * @param userId 用户ID
     * @return 代码记录
     */
    Optional<CodeRecord> getCodeRecord(Long id, Long userId);

    /**
     * 根据分享令牌获取代码记录
     * 
     * @param shareToken 分享令牌
     * @return 代码记录
     */
    Optional<CodeRecord> getCodeRecordByShareToken(String shareToken);

    /**
     * 获取用户代码记录列表
     * 
     * @param userId 用户ID
     * @param page 分页对象
     * @return 代码记录分页列表
     */
    IPage<CodeRecord> getUserCodeRecords(Long userId, Page<CodeRecord> page);
    
    /**
     * 获取用户代码记录列表（简化参数）
     * 
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页大小
     * @param language 编程语言过滤
     * @return 代码记录列表
     */
    List<CodeRecord> getUserCodeRecords(Long userId, int page, int size, String language);
    
    /**
     * 获取用户代码记录总数
     * 
     * @param userId 用户ID
     * @param language 编程语言过滤
     * @return 记录总数
     */
    long getUserCodeRecordCount(Long userId, String language);

    /**
     * 根据语言获取用户的代码记录
     * 
     * @param userId 用户ID
     * @param language 编程语言
     * @param pageable 分页参数
     * @return 代码记录分页
     */
    IPage<CodeRecord> getUserCodeRecordsByLanguage(Long userId, String language, Page<CodeRecord> page);

    /**
     * 搜索用户的代码记录
     * 
     * @param userId 用户ID
     * @param keyword 搜索关键词
     * @param pageable 分页参数
     * @return 代码记录分页
     */
    IPage<CodeRecord> searchUserCodeRecords(Long userId, String keyword, Page<CodeRecord> page);

    /**
     * 获取公开的代码记录
     * 
     * @param pageable 分页参数
     * @return 代码记录分页
     */
    IPage<CodeRecord> getPublicCodeRecords(Page<CodeRecord> page);

    /**
     * 根据语言获取公开的代码记录
     * 
     * @param language 编程语言
     * @param pageable 分页参数
     * @return 代码记录分页
     */
    IPage<CodeRecord> getPublicCodeRecordsByLanguage(String language, Page<CodeRecord> page);

    /**
     * 获取热门代码记录
     * 
     * @param pageable 分页参数
     * @return 代码记录分页
     */
    IPage<CodeRecord> getPopularCodeRecords(Page<CodeRecord> page);

    /**
     * 更新代码记录
     * 
     * @param id 记录ID
     * @param title 标题
     * @param code 代码内容
     * @param userId 用户ID
     * @return 更新后的代码记录
     */
    Optional<CodeRecord> updateCodeRecord(Long id, String title, String code, Long userId);

    /**
     * 删除代码记录
     * 
     * @param id 记录ID
     * @param userId 用户ID
     * @return 是否删除成功
     */
    boolean deleteCodeRecord(Long id, Long userId);

    /**
     * 分享代码记录
     * 
     * @param id 记录ID
     * @param isPublic 是否公开
     * @param allowEdit 是否允许编辑
     * @param userId 用户ID
     * @return 分享令牌
     */
    String shareCodeRecord(Long id, boolean isPublic, boolean allowEdit, Long userId);

    /**
     * 取消分享代码记录
     * 
     * @param id 记录ID
     * @param userId 用户ID
     * @return 是否取消成功
     */
    boolean unshareCodeRecord(Long id, Long userId);

    /**
     * 增加查看次数
     * 
     * @param id 记录ID
     */
    void incrementViewCount(Long id);

    /**
     * 增加执行次数
     * 
     * @param id 记录ID
     */
    void incrementExecutionCount(Long id);

    /**
     * 获取用户统计信息
     * 
     * @param userId 用户ID
     * @return 统计信息
     */
    UserCodeStatistics getUserStatistics(Long userId);

    /**
     * 获取系统统计信息
     * 
     * @return 统计信息
     */
    SystemCodeStatistics getSystemStatistics();

    /**
     * 获取语言使用统计
     * 
     * @return 语言统计列表
     */
    List<LanguageStatistics> getLanguageStatistics();

    /**
     * 获取用户最近的代码记录
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 代码记录列表
     */
    List<CodeRecord> getRecentCodeRecords(Long userId, int limit);

    /**
     * 清理过期的分享记录
     * 
     * @param expireDays 过期天数
     * @return 清理的记录数量
     */
    int cleanupExpiredSharedRecords(int expireDays);

    /**
     * 用户代码统计信息
     */
    class UserCodeStatistics {
        private Long totalRecords;
        private Long totalExecutions;
        private Long totalViews;
        private String mostUsedLanguage;
        private LocalDateTime lastActivity;
        private Map<String, Long> languageDistribution;

        // Getters and Setters
        public Long getTotalRecords() { return totalRecords; }
        public void setTotalRecords(Long totalRecords) { this.totalRecords = totalRecords; }

        public Long getTotalExecutions() { return totalExecutions; }
        public void setTotalExecutions(Long totalExecutions) { this.totalExecutions = totalExecutions; }

        public Long getTotalViews() { return totalViews; }
        public void setTotalViews(Long totalViews) { this.totalViews = totalViews; }

        public String getMostUsedLanguage() { return mostUsedLanguage; }
        public void setMostUsedLanguage(String mostUsedLanguage) { this.mostUsedLanguage = mostUsedLanguage; }

        public LocalDateTime getLastActivity() { return lastActivity; }
        public void setLastActivity(LocalDateTime lastActivity) { this.lastActivity = lastActivity; }

        public Map<String, Long> getLanguageDistribution() { return languageDistribution; }
        public void setLanguageDistribution(Map<String, Long> languageDistribution) { this.languageDistribution = languageDistribution; }
    }

    /**
     * 系统代码统计信息
     */
    class SystemCodeStatistics {
        private Long totalRecords;
        private Long totalUsers;
        private Long totalExecutions;
        private Long totalViews;
        private Long todayExecutions;
        private String mostPopularLanguage;

        // Getters and Setters
        public Long getTotalRecords() { return totalRecords; }
        public void setTotalRecords(Long totalRecords) { this.totalRecords = totalRecords; }

        public Long getTotalUsers() { return totalUsers; }
        public void setTotalUsers(Long totalUsers) { this.totalUsers = totalUsers; }

        public Long getTotalExecutions() { return totalExecutions; }
        public void setTotalExecutions(Long totalExecutions) { this.totalExecutions = totalExecutions; }

        public Long getTotalViews() { return totalViews; }
        public void setTotalViews(Long totalViews) { this.totalViews = totalViews; }

        public Long getTodayExecutions() { return todayExecutions; }
        public void setTodayExecutions(Long todayExecutions) { this.todayExecutions = todayExecutions; }

        public String getMostPopularLanguage() { return mostPopularLanguage; }
        public void setMostPopularLanguage(String mostPopularLanguage) { this.mostPopularLanguage = mostPopularLanguage; }
    }

    /**
     * 语言统计信息
     */
    class LanguageStatistics {
        private String language;
        private Long count;
        private Double percentage;

        public LanguageStatistics(String language, Long count) {
            this.language = language;
            this.count = count;
        }

        // Getters and Setters
        public String getLanguage() { return language; }
        public void setLanguage(String language) { this.language = language; }

        public Long getCount() { return count; }
        public void setCount(Long count) { this.count = count; }

        public Double getPercentage() { return percentage; }
        public void setPercentage(Double percentage) { this.percentage = percentage; }
    }
}